<?php
namespace app\controllers;


use app\models\distribution\DistributionBasic;
use app\models\group\GroupFans;
use app\models\group\GroupList;
use app\models\marketing\MarketingGroup;
use app\models\marketing\MarketingGroupLevel;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\services\Chain;
use app\util\Debug;
use yii\helpers\ArrayHelper;

class DemoController extends \app\controllers\Controller
{
    public function actionDemo()
    {
        $fans = GroupFans::find()
            ->where(['group_id' => 87])
            ->andWhere(['status' => 1])
            ->with(['order', 'order.item', 'order.item.product'])
            ->asArray()
            ->all();

        foreach ($fans as $item) {
            $cost = $item['order']['item'][0]['product']['price_cost'];
            $price = $item['order']['item'][0]['product']['price'];
            $quantity = $item['order']['item'][0]['quantity'];
            var_dump($cost, $price, $quantity);exit;
        }
    }

    public function actionDemo2()
    {
        $set = MarketingGroup::getLastOne();
        $win = $set->win_total;

        $ids = GroupFans::find()
            ->where(['group_id' => [91, 88, 83]])
            ->andWhere(['status' => 1])
            ->select(['id'])
            ->column();

        shuffle($ids);

        $ids = array_slice($ids, 0, $win);

        GroupFans::updateAll(['win' => 1], ['id' => $ids]);

        var_dump($ids);
    }

    public function actionDemo3()
    {
        $user = [
            [
                'id' => 1,
                'level' => 101
            ],
            [
                'id' => 2,
                'level' => 101
            ],
            [
                'id' => 3,
                'level' => 201
            ],
            [
                'id' => 4,
                'level' => 301
            ],
            [
                'id' => 5,
                'level' => 201
            ]
        ];

        $level = [
            [
                'value' => 101,
                'ratio' => 10
            ],
            [
                'value' => 201,
                'ratio' => 20
            ],
            [
                'value' => 301,
                'ratio' => 30
            ],
            [
                'value' => 401,
                'ratio' => 40
            ]
        ];

        $list = [];

        // foreach ($user as $u) {
        //     foreach ($level as $v) {
        //         if ($u['level'] == $v['value']) {
        //             array_push($list, [
        //                 'id' => $u['id'],
        //                 'ratio' => $v['ratio']
        //             ]);
        //         }
        //     }
        // }

        foreach ($level as &$v) {
            $v['user'] = [];
            foreach ($user as $u) {
                if ($u['level'] == $v['value']) {
                    // array_push($list, [
                    //     'id' => $u['id'],
                    //     'ratio' => $v['ratio']
                    // ]);
                    array_push($v['user'], $u['id']);
                }
            }
            if (count($v['user']) > 0) {
                array_push($list, [
                    'ratio' => $v['ratio'] / count($v['user']),
                    'user' => $v['user']
                ]);
            }
        }


        // $list = ArrayHelper::map($list, 'ratio', 'id');

        var_dump($list);
    }

    public function actionDemo6()
    {
        $fans_ids = GroupFans::find()
            ->where(['group_id' => 94])
            ->andWhere(['status' => 1])
            ->andWhere(['win' => 1])
            ->select(['user_id'])
            ->column();

        $user = User::find()
            ->where(['id' => $fans_ids])
            // ->with(['groupLevel0'])
            ->asArray()
            ->all();


        $level = MarketingGroupLevel::find()
            ->orderBy(['id' => SORT_ASC])
            ->asArray()
            ->all();

        $list = [];
        foreach ($level as &$item) {
            $item['user'] = [];

            foreach ($user as $u) {
                if ($u['group_level'] == $item['id']) {
                    array_push($item['user'], $u['id']);
                }
            }

            if (count($item['user']) > 0) {
                array_push($list, [
                    'ratio' => $item['ratio'] / count($item['user']) / 100,
                    'user' => $item['user']
                ]);
            }
        }

        $pool = GroupList::findOne(['group_id' => 94]);
        var_dump($list);
    }


    // 更新待返
    public function actionAwait()
    {
        $accounts = UserAccount::find()
            ->all();

        $str = '';

        foreach ($accounts as $account) {
            $sum = (float) UserCommission::find()
                ->where(['status' => 0])
                ->andWhere(['user_id' => $account->user_id])
                ->sum('balance');

            // echo $account->user_id, " [".$account->await_balance."] " . " (".$sum.") \n";

            $account->await_balance = $sum;

            if ($account->save()) {
                $str .= $account->user_id . " ... ok \n";
            }
        }

        return $str;
    }
}